﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Warranty.Data.DataContextStorage
{
    public class ThreadDataContextStorageContainer : IDataContextStorageContainer
    {
        private static readonly Hashtable dataContexts = new Hashtable();

        public WarrantyEntities GetDataContext()
        {
            WarrantyEntities libraryDataContext = null;
            if (dataContexts.Contains(GetThreadName()))
                libraryDataContext =
                (WarrantyEntities)dataContexts[GetThreadName()];
            return libraryDataContext;
        }

        public void Store(WarrantyEntities authentication)
        {
            var threadname = GetThreadName();
            if (!dataContexts.ContainsKey(threadname)) dataContexts.Add(threadname, authentication);
            else dataContexts[threadname] = authentication;
        }

        private static string GetThreadName()
        {
            return (Thread.CurrentThread == null || string.IsNullOrEmpty(Thread.CurrentThread.Name)) ? "WarrantyEntities" : Thread.CurrentThread.Name;
        }
    }
}
